package com.test.promanage.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.test.promanage.po.TableFile;
import com.test.promanage.po.TableProject;
import com.test.promanage.pojo.User;
import com.test.promanage.service.ProService;
import com.test.promanage.utils.TimeUtils;

@Controller
@RequestMapping("admin/active")
public class newsActivesController {

	@Autowired
	private ProService proService;
	@RequestMapping(value="getNewActive",method={RequestMethod.POST})
	public @ResponseBody List<ActivePojo> getNewActive(HttpServletRequest request ) throws Exception{
		User user=(User)request.getSession().getAttribute("user");
		List<ActivePojo> activePojos=new ArrayList<>();
		List<TableProject> tableProjects=proService.selectTableProjectByUid(user.getUserId());
		if(tableProjects!=null&&tableProjects.size()>0){
			for(TableProject tableProject:tableProjects){
				ActivePojo activePojo=new ActivePojo();
				activePojo.setTitle("创建项目");
				activePojo.setContent("项目名称："+tableProject.getProjectname());
				activePojo.setCreateTime(TimeUtils.changeDate(tableProject.getCreatetime()));
				activePojos.add(activePojo);
				activePojo.setTime(tableProject.getCreatetime().getTime());
			}
		}
		List<TableFile> tableFiles=proService.selectFileByUid(user.getUserId());
		if(tableFiles!=null&&tableFiles.size()>0){
			for(TableFile tableFile:tableFiles){
				ActivePojo activePojo=new ActivePojo();
				activePojo.setTitle("提交文件");
				activePojo.setCreateTime(TimeUtils.changeDate(tableFile.getCreatetime()));
				activePojo.setContent("文件名称："+tableFile.getFilename());
				activePojos.add(activePojo);
				activePojo.setTime(tableFile.getCreatetime().getTime());
			}
		}
		
		List<ActivePojo> activePojos2=new ArrayList<>();
		//进行排序
		if(activePojos!=null&&activePojos.size()>0){
			while(activePojos.size()>0){
				long ctime=activePojos.get(0).getTime();
				int index=0;
				int i=0;
				for(ActivePojo activePojo:activePojos){
					if(activePojo.getTime() > ctime){
						ctime=activePojo.getTime();
						index=i;
					}
					i++;
				}
				activePojos2.add(activePojos.get(index));
				activePojos.remove(index);
				
			}
			
		}
		
		return activePojos2;
	}
}
